var app = getApp();
var api = require('../../api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    payment_id:0,
    bank_account:'',
    bank_card:'',
    bank_openadd:'',
    is_default:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      // var id = options.id;
      // if (options.scene) {
      //   id = decodeURIComponent(options.scene);
      // }
      if (options.id){
        this.setData({
          payment_id: options.id
        })
      }
      
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.data.payment_id>0){
      this.getCardInfo();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var page = this
    var user = wx.getStorageSync('user_info');
    return {
      path: '/pages/index/index?pid=' + user.user_id,
    }
  },

  //获取卡片信息
  getCardInfo:function(){
    var page = this;
    app.request({
      url: api.user.card_info,
      method: 'POST',
      data: {
        card_id:page.data.payment_id
      },
      success: function (res) {
        if (res.errcode == 0) {
          page.setData({
            bank_account: res.data.bank_account,
            bank_card: res.data.bank_card,
            bank_openadd: res.data.bank_openadd,
            is_default: res.data.is_default
          });
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  },

  //户名输入
  getAccount: function (e) {
    this.setData({
      bank_account: e.detail.value
    });
  },

  //卡号输入
  getCard: function (e) {
    this.setData({
      bank_card: e.detail.value
    });
  },
  //开户行输入
  getOpenadd: function (e) {
    this.setData({
      bank_openadd: e.detail.value
    });
  },

  checkAll:function(e){
    var page = this

    if(e.detail.value.length>0){

      page.setData({
        is_default: 1
      });
    }else{
      page.setData({
        is_default: 0
      });
    }
  },

  getDefault: function (e) {
    console.log(e);
    this.setData({
      is_default: e.detail.value
    });
  },

  submitAdd:function(){
    var page = this;
    if (!page.data.bank_account || !page.data.bank_card || !page.data.bank_openadd){
      wx.showToast({
        title: '请填写完整信息',
        icon:'none'
      })
      return false;
    }else{
      page.addCard();
    }
  },

  addCard: function () {
    wx.showLoading({
      title: '提交中',
    })
    var page = this;
    app.request({
      url: api.user.add_card,
      method: 'POST',
      data: {
        id: page.data.payment_id,
        bank_account: page.data.bank_account,
        bank_card: page.data.bank_card,
        bank_openadd: page.data.bank_openadd,
        is_default: page.data.is_default,
      },
      success: function (res) {
        if (res.errcode == 0) {
          wx.showToast({
            title: '提交成功',
          });
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '警告',
          content: res.errmsg,
          showCancel: false
        })
      },
      complete: function () {
        wx.hideLoading();
      }
    });
  }
})